﻿using System.IO.Pipes;
using System.Security.Principal;

namespace PipeUtil.Pipe
{
    public class PipeClient
    {
        public string PipeName { get; private set; }
        public string ServerName { get; private set; }

        public PipeClient(string serverName, string pipeName)
        {
            PipeName = pipeName;
            ServerName = serverName;
        }

        public string Send(string message)
        {
            using (var pipeClient = new NamedPipeClientStream(ServerName, PipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation))
            {
                pipeClient.Connect(10000);
                var streamStringClient = new StreamString(pipeClient);
                streamStringClient.WriteString(message);
                var response = streamStringClient.ReadString();
                return response;
            }
            
        }
    }
}
